package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityViolationDeductMoneyOrderListBinding
import com.haoshuang.zhouzhoubang.model.bean.BalanceDetailGroupListBean
import com.haoshuang.zhouzhoubang.ui.adapter.ViolationDeductMoneyOrderListAdapter
import com.haoshuang.zhouzhoubang.ui.vm.ViolationDeductMoneyOrderListViewModel
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.vertical


/**
 * 违规扣款明细
 */
class ViolationDeductMoneyOrderListActivity :
    BaseVMActivity<ViolationDeductMoneyOrderListViewModel, ActivityViolationDeductMoneyOrderListBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<ViolationDeductMoneyOrderListActivity>()
        }
    }

    private var id = ""

    private val data = arrayListOf<BalanceDetailGroupListBean.BalanceDetailGroupBean>()
    private val adapter = ViolationDeductMoneyOrderListAdapter(data){item, position ->
        if (item?.order_id?.isNotEmpty() == true && item?.order_id != "0"){
            startPage<MxDetailActivity>("id" to item.id.toString())
        }
    }

    override fun initIntent() {
        id = intent.getString("id")
    }

    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "违规明细"
        binding.titleBar.leftImageView().click { finish() }

        binding.recyclerView.vertical().adapter = adapter
        adapter.setEmptyData()

        binding.refreshLayout.setOnRefreshListener {
            p = 1
            isShowing = false
            vm.masterBillGroupByDayList(id, p)
        }

        binding.refreshLayout.setOnLoadMoreListener {
            p++
            isShowing = false
            vm.masterBillGroupByDayList(id, p)
        }


    }

    override fun initData() {

        binding.refreshLayout.autoRefresh()

    }

    override fun startObserve() {

        vm.masterBillGroupByDayListData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it?.list, data, adapter, p)
            }
        }

    }

}